-- Author: roger
with Ada.Exceptions; use Ada.Exceptions;
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Integer_Text_Io; use Ada.Integer_Text_Io;
with PajekFileProcess; use PajekFilePRocess;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Command_line; use Ada.Command_Line;
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Maps;      use Ada.Strings.Maps;

procedure gradec is
    
    procedure WriteNodeLinkCount(filename: in String; network: in NodeListPointer) is
        OutputFile: File_Type;
        UnboundedName: Unbounded_String;
    begin
        -- Write node distribution file
        Ada.Text_IO.Create(
            File => OutputFile,
            Name => filename,
            Mode => Ada.Text_IO.Out_File);
        -- Write a line for each node
        for i in 1..network.nNodes loop
            Put(File => OutPutFile , Item => i, Width => 1);
            Put(File => OutPutFile , Item => "   "); 
            UnboundedName:=To_Unbounded_String(network.nodes(i).Name);
            Put(File => OutPutFile , Item => To_String(UnboundedName));
            Put(File => OutPutFile , Item => network.nodes(i).links, Width => 1);
            Put_Line(OutPutFile , "");         
        end loop;
        Ada.Text_IO.Close(File => OutPutFile);

    end WriteNodeLinkCount;
    
    procedure WriteGradeFrequencyCount(filename: in String; network: in NodeListPointer) is
        OutputFile: File_Type;
        grades: array(1..network.nNodes) of Integer;
    begin
        -- Initialize grades
        for i in 1..network.nNodes loop
            grades(i):=0;
        end loop;
        -- Grade count
        for i in 1..network.nNodes loop
            grades(network.nodes(i).links):= grades(network.nodes(i).links)+1;
        end loop;
        -- Write node grade file
        Ada.Text_IO.Create(
            File => OutputFile,
            Name => filename,
            Mode => Ada.Text_IO.Out_File);
        -- Write result to file
        for i in 1..network.nNodes loop
            if grades(i)>0 then
                Put(OutputFile,i);
                Put(OutputFile,grades(i));
                Put_Line(OutputFile,"");
            end if;
        end loop;
        Ada.Text_IO.Close(File => OutPutFile);
            
    end WriteGradeFrequencyCount;
       
    network: NodeListPointer; 
    filename: Unbounded_String; 
    first: Positive;
         
    BEGIN
        if Argument_count>0 then

            -- determine filename without extension
            first:=index(to_unbounded_string(Argument(1)),".net");
            filename:=head(to_unbounded_string(Argument(1)),first-1 ,' ');
     
            Put_Line("Parsing file: " & to_string(filename));
            -- Load Pajek File
            network:=LoadPajekFile(Argument(1));
            -- Write the node link count
            Put_Line("Node link count on: "& to_string(filename) & "-adagraph-graus.txt");
            WriteNodeLinkCount(to_string(filename) & "-agraph-graus.txt", network);
            -- Write the grades summary
            Put_Line("Frequency distribution summary on: " & to_string(filename) & "-adagraph-dist_grau.txt");
            WriteGradeFrequencyCount(to_string(filename) & "-agraph-dist_grau.txt",network);
        else
            Put_Line("Usage: gradec.exe <filename>.net");
        end if;
        exception
            when Name_Error =>
                Put(File => Standard_Error, Item => "File name not found.");
end gradec;
